﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace affmanager {
    public partial class Form1 : Form {
        #region Properties
        private string DataFile { get; set; }
        private List<Sites> SiteData { get; set; }
        #endregion

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            DataFile = "data.ini";
            SiteData = new List<Sites>(); //Prepare to store site data
            LoadData(true);

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
            SaveData();
        }

        private void siteToolStripMenuItem_Click(object sender, EventArgs e) {
            DialogResult dr = new DialogResult();
            AddNewSiteForm addSite = new AddNewSiteForm();
            dr = addSite.ShowDialog();
            if (dr == DialogResult.OK) {
                LoadData(false);
                MessageBox.Show("lol");
            }
        }

        private void LoadData(bool loadAtStartProgram) {
            sitesListView.Items.Clear();
            List<Sites> data = new List<Sites>();
            if (loadAtStartProgram) {
                HandleData.LoadAllSites(out data, DataFile);
                SiteData = data;
            }

            foreach (Sites Site in SiteData) {
                ListViewItem item = new ListViewItem(new string [] {Site.Name, Site.Url, Site.PR, 
                    Site.Age, Site.EarnYesterday, Site.EarnThisMonth, Site.EarnTotal});
                sitesListView.Items.Add(item);
            }
        }

        private void SaveData() { //Saves all the data
            HandleData.WriteAllSites(SiteData, DataFile);
        }

        private void reloadDataToolStripMenuItem_Click(object sender, EventArgs e) {
            LoadData(false);
        }
    }
}
